home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-tk / samples.tk / oglinfo.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  6KB  |  245 lines

  1. /*
  2.  * oglinfo.c 
  3.  */
  4.  
  5. /*
  6.  * This demo modified by BrianP to accomodate Mesa and test
  7.  * * the GLX 1.1 functions.
  8.  */
  9.  
  10. #include <GL/glx.h>
  11. #include <GL/gl.h>
  12. #include <GL/glu.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. int visual_request0[] =
  17. {None};                                           /*
  18.  
  19.                                             * don't need much of a visual 
  20.                                             */
  21. int visual_request1[] =
  22. {GLX_RGBA, None};                                   /*
  23.  
  24.                                             * in case CI failed 
  25.                                             */
  26.  
  27. int main(int argc, char **argv)
  28. {
  29.   char *display_name = NULL;
  30.   char *string;
  31.   Display *dpy;
  32.   int screen_num;
  33.   int major, minor;
  34.   XVisualInfo *vis;
  35.   GLXContext ctx;
  36.   Window root, win;
  37.   Colormap cmap;
  38.   XSetWindowAttributes swa;
  39.   int dontcare;
  40.  
  41.   /*
  42.    * parse arguments 
  43.    */
  44.   if (argc > 1)
  45.     if (!strcmp(argv[1], "-display"))
  46.       display_name = argv[2];
  47.     else {
  48.       fprintf(stderr, "Usage: %s [-display <display>]\n", argv[0]);
  49.       return -1;
  50.     }
  51.  
  52.   /*
  53.    * get display 
  54.    */
  55.   if (!(dpy = XOpenDisplay(display_name))) {
  56.     fprintf(stderr, "Error: XOpenDisplay() failed.\n");
  57.     return -1;
  58.   }
  59.  
  60.   /*
  61.    * does the server know about OpenGL & GLX? 
  62.    */
  63. #ifndef MESA
  64.   if (!XQueryExtension(dpy, "GLX", &dontcare, &dontcare, &dontcare)) {
  65.     fprintf(stderr, "This system doesn't appear to support OpenGL\n");
  66.     return -1;
  67.   }
  68. #endif
  69.  
  70.   /*
  71.    * find the glx version 
  72.    */
  73.   if (glXQueryVersion(dpy, &major, &minor))
  74.     printf("GLX Version: %d.%d\n", major, minor);
  75.   else {
  76.     fprintf(stderr, "Error: glXQueryVersion() failed.\n");
  77.     return -1;
  78.   }
  79.  
  80.   /*
  81.    * get screen number 
  82.    */
  83.   screen_num = DefaultScreen(dpy);
  84.  
  85. /*
  86.  * This #ifdef isn't redundant. It keeps the build from breaking
  87.  * ** if you are building on a machine that has an old (1.0) version
  88.  * ** of glx.
  89.  * **
  90.  * ** This program could still be *run* on a machine that has an old 
  91.  * ** version of glx, even if it was *compiled* on a version that has
  92.  * ** a new version.
  93.  * **
  94.  * ** If compiled on a system with an old version of glx, then it will 
  95.  * ** never recognize glx extensions, since that code would have been
  96.  * ** #ifdef'ed out.
  97.  */
  98. #ifdef GLX_VERSION_1_1
  99.  
  100.   /*
  101.    * ** This test guarantees that glx, on the display you are inquiring,
  102.    * ** suppports glXQueryExtensionsString().
  103.    */
  104.   if (minor > 0 || major > 1)
  105.     string = (char *)glXQueryExtensionsString(dpy, screen_num);
  106.   else
  107.     string = "";
  108.  
  109.   if (string)
  110.     printf("GLX Extensions (client & server): %s\n",
  111.        string);
  112.   else {
  113.     fprintf(stderr, "Error: glXQueryExtensionsString() failed.\n");
  114.     return -1;
  115.   }
  116.  
  117.   if (minor > 0 || major > 1) {
  118.     printf("glXGetClientString(GLX_VENDOR): %s\n", glXGetClientString(dpy, GLX_VENDOR));
  119.     printf("glXGetClientString(GLX_VERSION): %s\n", glXGetClientString(dpy, GLX_VERSION));
  120.     printf("glXGetClientString(GLX_EXTENSIONS): %s\n", glXGetClientString(dpy, GLX_EXTENSIONS));
  121.     printf("glXQueryServerString(GLX_VENDOR): %s\n", glXQueryServerString(dpy, screen_num, GLX_VENDOR));
  122.     printf("glXQueryServerString(GLX_VERSION): %s\n", glXQueryServerString(dpy, screen_num, GLX_VERSION));
  123.     printf("glXQueryServerString(GLX_EXTENSIONS): %s\n", glXQueryServerString(dpy, screen_num, GLX_EXTENSIONS));
  124.   }
  125.  
  126. #endif
  127.  
  128.   /*
  129.    * get any valid OpenGL visual 
  130.    */
  131.   if (!(vis = glXChooseVisual(dpy, screen_num, visual_request0))) {
  132.     if (!(vis = glXChooseVisual(dpy, screen_num, visual_request1))) {
  133.       fprintf(stderr, "Error: glXChooseVisual() failed.\n");
  134.       return -1;
  135.     }
  136.   }
  137.  
  138.   /*
  139.    * get context 
  140.    */
  141.   ctx = glXCreateContext(dpy, vis, 0, GL_TRUE);
  142.  
  143.   /*
  144.    * root window 
  145.    */
  146.   root = RootWindow(dpy, vis->screen);
  147.  
  148.   /*
  149.    * get RGBA colormap 
  150.    */
  151.   cmap = XCreateColormap(dpy, root, vis->visual, AllocNone);
  152.  
  153.   /*
  154.    * get window 
  155.    */
  156.   swa.colormap = cmap;
  157.   swa.border_pixel = 0;
  158.   swa.event_mask = StructureNotifyMask;
  159.   win = XCreateWindow(dpy, root, 0, 0, 1, 1, 0, vis->depth,
  160.               InputOutput, vis->visual,
  161.               CWBorderPixel | CWColormap | CWEventMask,
  162.               &swa);
  163.  
  164.   glXMakeCurrent(dpy, win, ctx);
  165.  
  166.   string = (char *)glGetString(GL_VERSION);
  167.   if (string)
  168. #ifdef MESA
  169.     printf("Mesa Version: %s\n", string);
  170. #else
  171.     printf("OpenGL Version: %s\n", string);
  172. #endif
  173.   else {
  174.     fprintf(stderr, "Error: glGetString(GL_VERSION) failed.\n");
  175.     return -1;
  176.   }
  177.  
  178.   string = (char *)glGetString(GL_EXTENSIONS);
  179.  
  180.   if (string)
  181. #ifdef MESA
  182.     printf("Mesa Extensions: %s\n", string);
  183. #else
  184.     printf("OpenGL Extensions: %s\n", string);
  185. #endif
  186.   else {
  187.     fprintf(stderr, "Error: glGetString(GL_EXTENSIONS) failed.\n");
  188.     return -1;
  189.   }
  190.  
  191.   string = (char *)glGetString(GL_RENDERER);
  192.  
  193.   if (string)
  194. #ifdef MESA
  195.     printf("Mesa Renderer: %s\n", string);
  196. #else
  197.     printf("OpenGL renderer: %s\n", string);
  198. #endif
  199.   else {
  200.     fprintf(stderr, "Error: glGetString(GL_RENDERER) failed.\n");
  201.     return -1;
  202.   }
  203.  
  204. /*
  205.  * ** This #ifdef prevents a build failure if you compile on an a
  206.  * ** machine with an old GLU library. 
  207.  * **
  208.  * ** If you build on a pre GLU 1.1 machine, you will never be able
  209.  * ** to get glu info, even if you run on a GLU 1.1 or latter machine,
  210.  * ** since the code has been #ifdef'ed out.
  211.  */
  212. #ifdef GLU_VERSION_1_1
  213.  
  214.   /*
  215.    * ** If the glx version is 1.1 or latter, gluGetString() is guaranteed
  216.    * ** to exist.
  217.    */
  218.   if (minor > 0 || major > 1)
  219.     string = (char *)gluGetString(GLU_VERSION);
  220.   else
  221.     string = "1.0";
  222.  
  223.   if (string)
  224.     printf("GLU Version: %s\n", string);
  225.   else {
  226.     fprintf(stderr, "Error: gluGetString(GLU_VERSION) failed.\n");
  227.     return -1;
  228.   }
  229.  
  230.   if (minor > 0 || major > 1)
  231.     string = (char *)gluGetString(GLU_EXTENSIONS);
  232.   else
  233.     string = "";
  234.  
  235.   if (string)
  236.     printf("GLU Extensions: %s\n", string);
  237.   else {
  238.     fprintf(stderr, "Error: gluGetString(GLU_EXTENSIONS) failed.\n");
  239.     return -1;
  240.   }
  241.  
  242. #endif
  243.   return 0;
  244. }
  245.